package patrol.mobile.com.mobilepatrol.activity.road_block;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;

import java.util.List;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.DBHelper;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.adapter.BlockListAdapter;
import patrol.mobile.com.mobilepatrol.fragment.BaseFragment;
import patrol.mobile.com.mobilepatrol.utils.UIHelper;

/**
 * Created by apple on 18/1/25.
 */

public class EditRoadBlockListFragment extends BaseFragment {
    @BindView(R.id.recycler)
    RecyclerView recycler;

    @BindView(R.id.tv_add)
    TextView tv_add;
    private List<AssetTongzuxinxi> mList;
    private DBHelper db;
    private BlockListAdapter mAdapter;

    protected int getLayoutId() {
        return R.layout.edit_block_activity;
    }

    @Override
    protected void initView() {
        super.initView();
        db = new DBHelper(getActivity());
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void initData() {
        mList = db.getAssetTongs();
        mAdapter = new BlockListAdapter((RoadBlockActivity)getActivity(), mList);
        recycler.setAdapter(mAdapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        mList = db.getAssetTongs();
        mAdapter.updateItems(mList);

    }

    @Override
    protected void initListener() {
        tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),EidtBlockActivity.class);
                intent.putExtra("ui_role",1);
                startActivity(intent);
            }
        });
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(),EidtBlockActivity.class);
                intent.putExtra("ui_role",2);
                intent.putExtra(UIHelper.ATongzuxinxi,mList.get(position));
                startActivity(intent);
            }
        });
    }
    public void refreshData(){
        mList = db.getAssetTongs();
        mAdapter.updateItems(mList);
    }
}
